package api

import (
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"web_api_gogf/app/service"
	"web_api_gogf/library/response"
)

var User = UserApi{}

type UserApi struct {
}

// Current 获取当前用户信息
func (u *UserApi) Current(r *ghttp.Request) {
	userData, err := service.SysUser.AuthorizationGetUserData(r)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	if userData == nil {
		response.JsonExit(r, 1, "用户不存在")
	}
	response.Json(r, 0, "获取成功", g.Map{
		"user": userData,
	})
}

// GetConfig 获取用户配置
func (u *UserApi) GetConfig(r *ghttp.Request) {
	userData, err := service.SysUser.AuthorizationGetUserData(r)
	if err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	if userData == nil {
		response.JsonExit(r, 1, "用户不存在")
	}
	userRoleNameList, userPermNameList, err := service.SysUser.GetAdminUserConf(userData)
	if err != nil {
		response.JsonExit(r, 1, "角色列表异常")
	}
	menuData, err := service.SysMenu.GetHasPermMenuList("admin", userPermNameList)
	if err != nil {
		response.JsonExit(r, 1, "菜单获取异常")
	}
	response.Json(r, 0, "获取成功", g.Map{
		"user":           userData,
		"roleList":       userRoleNameList,
		"permissionList": userPermNameList,
		"menuData":       menuData,
	})
}

//当前用户文章列表
func (u *UserApi) FakeList(r *ghttp.Request) {
	str := `{
  "id": "fake-list-0",
  "owner": "付小小",
  "title": "Alipay",
  "avatar": "https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png",
  "cover": "https://gw.alipayobjects.com/zos/rmsportal/uMfMFlvUuceEyPpotzlq.png",
  "status": "active",
  "percent": 58,
  "logo": "https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png",
  "href": "https://ant.design",
  "updatedAt": "2021-06-02T03:22:56.646Z",
  "createdAt": "2021-06-02T03:22:56.646Z",
  "subDescription": "那是一种内在的东西， 他们到达不了，也无法触及的",
  "description": "在中台产品的研发过程中，会出现不同的设计规范和实现方式，但其中往往存在很多类似的页面和组件，这些类似的组件会被抽离成一套标准规范。",
  "activeUser": 122849,
  "newUser": 1696,
  "star": 111,
  "like": 113,
  "message": 12,
  "content": "段落示意：蚂蚁金服设计平台 ant.design，用最小的工作量，无缝接入蚂蚁金服生态，提供跨越设计与开发的体验解决方案。蚂蚁金服设计平台 ant.design，用最小的工作量，无缝接入蚂蚁金服生态，提供跨越设计与开发的体验解决方案。",
  "members": [
    {
      "avatar": "https://gw.alipayobjects.com/zos/rmsportal/ZiESqWwCXBRQoaPONSJe.png",
      "name": "曲丽丽",
      "id": "member1"
    },
    {
      "avatar": "https://gw.alipayobjects.com/zos/rmsportal/tBOxZPlITHqwlGjsJWaF.png",
      "name": "王昭君",
      "id": "member2"
    },
    {
      "avatar": "https://gw.alipayobjects.com/zos/rmsportal/sBxjgqiuHMGRkIjqlQCd.png",
      "name": "董娜娜",
      "id": "member3"
    }
  ]
}`
	var list []interface{}
	data, _ := gjson.Decode(str)
	for i := 0; i < 20; i++ {
		list = append(list, data)
	}
	response.Json(r, 0, "加载成功", g.Map{
		"list": list,
	})
}
